<?php

class Metalstorm {

    public $file = 'data/metalstorm/metalstorm.tmp';
    public $url = 'http://www.metalstorm.ee/events/new_releases.php?upcoming=1';

    public function getReleases() {

        if(!file_exists($this->file)) {

            $f = fopen($_SERVER['DOCUMENT_ROOT'].$this->file, 'w');

            $data = file_get_contents($this->url);

            fwrite($f, $data);
            fclose($f);

        } else {

            $data = file_get_contents($_SERVER['DOCUMENT_ROOT'].$this->file, FILE_USE_INCLUDE_PATH);
        }

        $result = array();

        preg_match_all("/([0-9][0-9]\.[0-9][0-9])(\s*\&nbsp;*)+<\/span><a href=\/bands\/album\.php\?album_id=[0-9]{5}>[a-z0-9\s]+\s\-[a-z\s0-9]+/i", $data, $matches);

        foreach($matches[0] as $line) {

            $date = current(explode(' ', $line));

            preg_match('/[a-z0-9\s]+\s\-[a-z0-9\s]+/i', $line, $ll);

            list($artist, $release) = explode('-', $ll[0], 2);

            $result[] = array(
                'date'=>trim($date),
                'artist'=>trim($artist),
                'release'=>trim($release));

        }

        return $result;

    }

}